"""
数据处理层
"""
import os
import json
from conf import settings


# 查看数据
def select(username):
    # 拼接用户文 件名
    user_path = os.path.join(
        settings.USER_DATA_DIR, f'{username}.json'
    )
    # 判断用户是否存在
    if os.path.exists(user_path):
        with open(user_path, 'r', encoding='utf-8') as f:
            user_dic = json.load(f)
            return user_dic
    return None


# 保存数据
def save(user_dic):
    # 获取用户名拼接路径
    username = user_dic.get('username')
    user_path = os.path.join(
        settings.USER_DATA_DIR, f'{username}.json'
    )

    # 保存数据
    with open(user_path, 'w', encoding='utf-8') as f:
        json.dump(user_dic, f,ensure_ascii=False)
